package org.lanqiao.tjut.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.bean.AdminBean;
import org.lanqiao.tjut.bean.StudentBean;
import org.lanqiao.tjut.bean.TeacherBean;
import org.lanqiao.tjut.myUtils.MyUtils;
import org.lanqiao.tjut.service.ServiceFactory;

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	// @Override
	// protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	// throws ServletException, IOException {
	// // 响应客户端的get请求 --- 一般讲doget和dopost串联起来进行处理
	// super.doGet(req, resp);
	// }
	//
	// @Override
	// protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	// throws ServletException, IOException {
	// // 响应客户端的post请求
	// //调用Model层进行客户端请求的响应处理
	// super.doPost(req, resp);
	// }

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 对客户端请求进行处理 （可以替代 doget和dopost）
		// 调用Model层进行客户端请求的响应处理
		// super.service(req, resp);
		// System.out.println("已经接收到客户端发送过来的请求service.......");

		// 接收客户端数据请求是进行编码设置，防止中文乱码
		// req.setCharacterEncoding("UTF-8");
		// 用户名
		String strUserName = req.getParameter("txtUserName");
		// 密码
		String strUserPsw = req.getParameter("txtUserPsw");
		// 用户身份ID
		String strUserId = req.getParameter("selectId");
		// 将用户信息充实到实体对象属性中去
		// 学生登录
		if (strUserId.trim().equals("管理员")) {
			AdminBean admin = (AdminBean) MyUtils.getNewInsdence(AdminBean.class);
			admin.setAdminname(strUserName);
			admin.setAdminpsw(strUserPsw);
			admin.setAdminideftity(strUserId);
			List<AdminBean> lstUser = ServiceFactory.getLoginServiceInstance().isLoginedA(admin);

			if (lstUser != null & lstUser.size() > 0) {
				// 登录成功
				// 输入日志
				MyUtils.logger_rootLogger.info("管理员" + admin.getAdminname() + "登录成功");

				req.getSession().setAttribute("LoginedUserA", lstUser.get(0));
				// 使用forward进行页面跳转
				req.getRequestDispatcher("Logined/LoginedA.jsp").forward(req, resp);
			} else {
				req.getRequestDispatcher("Login.jsp").forward(req, resp);
			}

		} else if (req.getParameter("selectId").trim().equals("教师")) {
			TeacherBean tea = (TeacherBean) MyUtils.getNewInsdence(TeacherBean.class);
			tea.setTeaname(strUserName);
			tea.setTeapsw(strUserPsw);
			tea.setTeaideftity(strUserId);
			List<TeacherBean> lstUser = ServiceFactory.getLoginServiceInstance().isLoginedT(tea);

			if (lstUser != null & lstUser.size() > 0) {
				// 登录成功
				// 输入日志
				MyUtils.logger_rootLogger.info("教师" + tea.getTeaname() + "登录成功");
				req.getSession().setAttribute("LoginedUserT", lstUser.get(0));

				// 查询该老师信息集合
				List<TeacherBean> lstT = ServiceFactory.getUserInfoManagerSericeInstance().getQueryTeaInfo(tea);
				// 将查询的用户信息集合放入session缓存中
				req.getSession().setAttribute("lstT", lstT);

				// 使用forward进行页面跳转
				req.getRequestDispatcher("Logined/LoginedT.jsp").forward(req, resp);
				// resp.sendRedirect("Logined/LoginedT.jsp");
			} else {
				req.getRequestDispatcher("Login.jsp").forward(req, resp);
			}

		} else if (req.getParameter("selectId").trim().equals("学生")) {
			StudentBean stu = (StudentBean) MyUtils.getNewInsdence(StudentBean.class);
			stu.setStuname(strUserName);
			stu.setStupsw(strUserPsw);
			stu.setStuideftity(strUserId);
			List<StudentBean> lstUser = ServiceFactory.getLoginServiceInstance().isLoginedS(stu);
			if (lstUser != null & lstUser.size() > 0) {
				// 登录成功
				// 输入日志
				MyUtils.logger_rootLogger.info("学生" + stu.getStuname() + "登录成功");
				req.getSession().setAttribute("LoginedUserS", lstUser.get(0));

				// 查询该学生信息集合
				List<StudentBean> lstS = ServiceFactory.getUserInfoManagerSericeInstance().getQueryStuInfo(stu);
				// 将查询的用户信息集合放入session缓存中
				req.getSession().setAttribute("lstS", lstS);
				// 使用forward进行页面跳转
				req.getRequestDispatcher("Logined/LoginedS.jsp").forward(req, resp);
			} else {
				req.getRequestDispatcher("Login.jsp").forward(req, resp);
			}
		}
	}

	@Override
	public void destroy() {
		// Servlet销毁时的处理
		super.destroy();
	}

	@Override
	public void init() throws ServletException {
		// servlet初始化时的处理
		super.init();
	}

}
